var express = require("express");
var mongoose = require("mongoose");
var bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({
  extended:true
}));
var canvas_content = require("./model").canvas_content
var canvas_authority = require("./model").canvas_authority
app.post("/save",function (req, res,next) {
  var canvasName = req.body.canvasName;
  var  canvasId = req.body.canvasId;
  var  creator = req.body.creator;
  var lists0 = req.body.lists0.split(',');
  var lists1 = req.body.lists1.split(',');
  var lists2 = req.body.lists2.split(',');
  var userImageurl = req.body.userImageurl;
  var stat = req.body.stat;
  console.log(req.body);
  var editInfo = JSON.parse(req.body.editInfo);
  console.log(editInfo.length);
  console.log(typeof(req.body.editInfo))
  console.log(JSON.parse(req.body.editInfo));
  var list0 = [];
  var list1 = [];
  var list2 = [];
  for(var o in editInfo){
    if(editInfo[o].index1 == 0){
      var newData ={"editor":editInfo[o].editor, "content":lists0[editInfo[o].index2]};
      list0.push(newData);
    }
    else if(editInfo[o].index1 == 1){
      var newData ={"editor":editInfo[o].editor, "content":lists1[editInfo[o].index2]};
      list1.push(newData);
    }else if(editInfo[o].index1 == 2){
      var newData ={"editor":editInfo[o].editor, "content":lists2[editInfo[o].index2]};
      list2.push(newData);
    }else{
      console.log("err:meiyouduiyingdeneirong");
    }

  }

  mongoose.connect("mongodb://localhost/canvas",function (err) {
      if(err){
        console.log("连接失败");
      }else{
        console.log("连接成功");
//io代替查询修改部分，有待改善
        canvas_content.remove({creator:creator,canvasName:canvasName},function (error,result){
            if (error) return handleError(error);
            console.log("result" + result)
          }

        )
        var doc1 = new canvas_content({canvasId: canvasId,canvasName: canvasName,creator:creator,lists0:list0,lists1:list1,lists2:list2})
        doc1.save(function (err, doc) {
          if(err){
            console.log(err);
          }else{
            console.log(doc);
          }
        })
        if(stat ==null|| stat =="new"){
          var doc2 = new canvas_authority({canvasName:canvasName,creator:creator,userImageurl:userImageurl})
          doc2.save(function (err, doc) {
            if(err){
              console.log(err);
            }else{
              console.log(doc);
            }
          })
        }
      }
    }

  );
})

app.post("/downloadList",function(req,res,next){
  var responseResult = [];
  var nickName = req.body.nickName;
  mongoose.connect("mongodb://localhost/canvas",function (err) {
    if (err) {
      console.log("连接失败");
    } else {
      console.log("连接成功");
      canvas_authority.find({ $or: [ { creator: nickName },  { invitee: { $elemMatch: { "name": nickName} } }]},function (err, result) {
        if (err) return handleError(err);
        res.send(result);
        console.log("result"+result)
      }) }
  })
})
app.post("/requestList",function(req,res,next){
  var nickName = req.body.nickName;
  console.log(typeof(nickName));
  console.log("./requestList"+nickName)
  mongoose.connect("mongodb://localhost/canvas",function (err) {
    if (err) {
      console.log("连接失败");
    } else {
      console.log("连接成功");
      canvas_authority.find({creator:req.body.nickName},{canvasName:1}, function (err, result) {
        if (err) return handleError(err);

        res.send(result);
        console.log("result" + result)
      })
    }

  })
})
//requstForcontent, 请求画布内容
app.post("/requestForcontent",function(req,res,next) {
  var canvasName = req.body.canvasName;
  var creator = req.body.creator;
  mongoose.connect("mongodb://localhost/canvas", function (err) {
    if (err) {
      console.log("连接失败");
    } else {
      console.log("连接成功");
      canvas_content.find({creator: creator, canvasName:canvasName },function (err, result) {
        if(err){
          console.log(err)
        }else {
          res.send(result);
          console.log("requestForcontent" + result);
        }
      })
    }
  })
})
//添加被邀请者名单
app.post("/addInvitee",function (req,res,next) {
  var canvasName = req.body.canvasName;
  var creator = req.body.creator;
  var invitee = req.body.invitee;
  mongoose.connect("mongodb://localhost/canvas", function (err) {
    if (err) {
      console.log("连接失败");
    } else {
      console.log("连接成功");
      var conditions ={creator:creator,canvasName:canvasName};
      var update ={'$push':{invitee:{name:invitee}}};
      canvas_authority.update(conditions,update,function (err, result) {
        if(err){
          console.log(err);
        }else{
          res.send(result);
          console.log("update success!")
        }
      })
    }
  })
})
app.listen(8765, () => console.log('running on port 8765...'))

